#ifndef THREAD_H
#define THREAD_H

namespace Library
{

class PLIBEXP Thread
{
	public:
							Thread(HINSTANCE hInstance, HWND hWnd,
								const String & name);
		virtual				~Thread();

				enum State { TS_RUNNING, TS_SUSPENDED, TS_STOPPED };
				int32		state();

				bool8		resume();
				bool8		suspend();
				bool8		stop();
				bool8		kill();

	protected:
				void		beginCS();
				void		endCS();

				enum Message { TM_START = WM_USER + 1, TM_STOP, TM_NEXT };
		virtual int32		windowProc(HWND, UINT, WPARAM, LPARAM) = 0;

	private:
				bool8		createWindow();
				void		destroyWindow();

	protected:
		String				m_name;
		int32				m_state;
		HANDLE				m_thread;
		UINT				m_threadId;
		HWND				m_self;
		HWND				m_parent;
		HINSTANCE			m_hinstance;
		ATOM				m_class;
		CRITICAL_SECTION 	m_mutex;

	private:
		static UINT WINAPI s_threadProc(void *);
		static LRESULT CALLBACK	s_windowProc(HWND, UINT, WPARAM, LPARAM);

	// prohibited
	private:
		Thread();
		Thread(Thread &);
		Thread & operator=(Thread &);
};

//----------------------------------------------------------------------------
// Inline
//
inline int32
Thread::state()
{
	return m_state;
}

inline void
Thread::beginCS()
{
	EnterCriticalSection(&m_mutex);
}

inline void
Thread::endCS()
{
	LeaveCriticalSection(&m_mutex);
}

}; // namespace Library

#endif
